//
//  MainTableViewController.swift
//  新浪微博
//
//  Created by test on 15/7/27.
//  Copyright © 2015年 test. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
    
        
    }
    
    
    
    override func viewDidAppear(animated: Bool) {
       
       setupComposedButton()
    }
    //添加所有的控制器
    private  func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imgName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imgName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我", imgName: "tabbar_profile")
        
        
    }
    
    //
    //添加子控制器、、
    private  func addChildViewController(vc:UIViewController,title:String,imgName:String) {
        tabBar.tintColor = UIColor.orangeColor()
        vc.title = title;
        vc.tabBarItem.image = UIImage(named: imgName)
        
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
  
    }
    lazy private var componseButton : UIButton = {
      let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
    
    
        return btn
    
    }()
    
    func btnClick() {
    
    print(__FUNCTION__)
    
    }
    
    //添加撰写按钮
    private  func setupComposedButton() {
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRectMake(0, 0, w, tabBar.bounds.height)
        
        
        componseButton.frame = CGRectOffset(rect, 2 * w, 0)
    
    }
    
}
